<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/jexo/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/jexo/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/jexo/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/jexo/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/jexo/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/jexo/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/jexo/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Android,RxJava,源码解析," />










<meta name="description" content="Buffer操作符功能介绍如果本来是Observable&amp;lt;String&amp;gt;的一个对象，那么我只能在onNext中一个一个处理String,通过Buffer操作符，我们可以把n个String组合在一起，然后在onNext中进行处理">
<meta name="keywords" content="Android,RxJava,源码解析">
<meta property="og:type" content="article">
<meta property="og:title" content="我是用来组合的操作符------Buffer">
<meta property="og:url" content="http://ja_project.gitee.io/jexo/2018/04/18/我是用来组合的操作符-Buffer/index.html">
<meta property="og:site_name" content="Ling Hu Blog">
<meta property="og:description" content="Buffer操作符功能介绍如果本来是Observable&amp;lt;String&amp;gt;的一个对象，那么我只能在onNext中一个一个处理String,通过Buffer操作符，我们可以把n个String组合在一起，然后在onNext中进行处理">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-04-18T13:47:38.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="我是用来组合的操作符------Buffer">
<meta name="twitter:description" content="Buffer操作符功能介绍如果本来是Observable&amp;lt;String&amp;gt;的一个对象，那么我只能在onNext中一个一个处理String,通过Buffer操作符，我们可以把n个String组合在一起，然后在onNext中进行处理">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/jexo/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://ja_project.gitee.io/jexo/2018/04/18/我是用来组合的操作符-Buffer/"/>





  <title>我是用来组合的操作符------Buffer | Ling Hu Blog</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?abc8796c232e2b898d516245257842b4";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/jexo/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Ling Hu Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/jexo/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/jexo/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/jexo/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/jexo/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/jexo/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://ja_project.gitee.io/jexo/jexo/2018/04/18/我是用来组合的操作符-Buffer/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Ling Hu">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/jexo/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Ling Hu Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">我是用来组合的操作符------Buffer</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-04-18T21:47:15+08:00">
                2018-04-18
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/jexo/categories/RxJava操作符源码解析/" itemprop="url" rel="index">
                    <span itemprop="name">RxJava操作符源码解析</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/jexo/2018/04/18/我是用来组合的操作符-Buffer/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count gitment-comments-count" data-xid="/jexo/2018/04/18/我是用来组合的操作符-Buffer/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h5 id="Buffer操作符功能介绍"><a href="#Buffer操作符功能介绍" class="headerlink" title="Buffer操作符功能介绍"></a>Buffer操作符功能介绍</h5><p>如果本来是<code>Observable&lt;String&gt;</code>的一个对象，那么我只能在<code>onNext</code>中一个一个处理<code>String</code>,通过<code>Buffer</code>操作符，我们可以把<code>n</code>个<code>String</code>组合在一起，然后在<code>onNext</code>中进行处理</p>
<a id="more"></a>
<h5 id="用途"><a href="#用途" class="headerlink" title="用途"></a>用途</h5><p>可以操作一些需要一起处理的数据。比如2个数据为一组，我始终要打印最大的数据，或者3个数据为一组，打印最大值。</p>
<h5 id="来一段代码"><a href="#来一段代码" class="headerlink" title="来一段代码"></a>来一段代码</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">btSub.setOnClickListener(&#123;</span><br><span class="line">           observable?.subscribe(&#123; msg -&gt;</span><br><span class="line">               tvContent.text = tvContent.text.toString() + <span class="string">"\n"</span> + msg.toString()</span><br><span class="line">           &#125;)</span><br><span class="line">       &#125;)</span><br><span class="line">       observable = Observable.just(<span class="string">"test1"</span>,<span class="string">"test2"</span>,<span class="string">"test3"</span>).buffer(<span class="number">2</span>)</span><br></pre></td></tr></table></figure>
<p>代码的作用就是每2个字符串 打印在一行文本中。</p>
<h5 id="先来学习下just是什么鬼"><a href="#先来学习下just是什么鬼" class="headerlink" title="先来学习下just是什么鬼"></a>先来学习下<code>just</code>是什么鬼</h5><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"> Observable.create(object : Observable.OnSubscribe&lt;String&gt; &#123;</span><br><span class="line">            <span class="function">override fun <span class="title">call</span><span class="params">(t: Subscriber&lt;in String&gt;)</span> </span>&#123;</span><br><span class="line">                t.onNext(<span class="string">"test1"</span>)</span><br><span class="line">                t.onNext(<span class="string">"test2"</span>)</span><br><span class="line">                t.onNext(<span class="string">"test3"</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">....</span><br><span class="line"> Observable.just(<span class="string">"test1"</span>,<span class="string">"test2"</span>,<span class="string">"test3"</span>)</span><br></pre></td></tr></table></figure>
<p>这2行代码的效果其实是一致的(当我们，深入源代码的时候，其实会发现有一些不同，但是我们现在先一个个学习操作符，然后再会过来看这些，其实会非常的简单)<br>这里我就不对<code>just</code>进行深入了，主要以操作符为主，如果感兴趣的可以自己看看。</p>
<h5 id="看看源代码"><a href="#看看源代码" class="headerlink" title="看看源代码"></a>看看源代码</h5><p>Observable<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> Observable&lt;List&lt;T&gt;&gt; buffer(<span class="keyword">int</span> count) &#123;</span><br><span class="line">        <span class="keyword">return</span> buffer(count, count);</span><br><span class="line">    &#125;</span><br><span class="line">...</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">final</span> Observable&lt;List&lt;T&gt;&gt; buffer(<span class="keyword">int</span> count, <span class="keyword">int</span> skip) &#123;</span><br><span class="line">        <span class="keyword">return</span> lift(<span class="keyword">new</span> OperatorBufferWithSize&lt;T&gt;(count, skip));</span><br><span class="line">    &#125;</span><br><span class="line">...</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">final</span> &lt;R&gt; <span class="function">Observable&lt;R&gt; <span class="title">lift</span><span class="params">(<span class="keyword">final</span> Operator&lt;? extends R, ? <span class="keyword">super</span> T&gt; operator)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> unsafeCreate(<span class="keyword">new</span> OnSubscribeLift&lt;T, R&gt;(onSubscribe, operator));</span><br><span class="line">    &#125;</span><br><span class="line">...</span><br></pre></td></tr></table></figure></p>
<p>这里我们看到了一个很牛逼的东西，也就是<code>lift</code>，在<a href="https://www.jianshu.com/p/03a72a5a2049" target="_blank" rel="noopener">前言</a>中，其实我也给大家推荐了一篇关于<code>RxJava</code>的文章，写的很经典，大家最好先看看。</p>
<p>既然提到了<code>lift</code>那么就不得不提到<code>Operator</code>接口了。前面其实我也已经介绍过，<code>RxJava</code>中最重要的4个类或接口，如果忘了，或者不太了解的，大家可以再去看下那<br>篇文章<a href="https://www.jianshu.com/p/e1d08ebd1571" target="_blank" rel="noopener">我是最简单的操作符—–Create</a></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"> <span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Operator</span>&lt;<span class="title">R</span>, <span class="title">T</span>&gt; <span class="keyword">extends</span> <span class="title">Func1</span>&lt;<span class="title">Subscriber</span>&lt;? <span class="title">super</span> <span class="title">R</span>&gt;, <span class="title">Subscriber</span>&lt;? <span class="title">super</span> <span class="title">T</span>&gt;&gt; </span>&#123;</span><br><span class="line">        <span class="comment">// cover for generics insanity</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Func1</span>&lt;<span class="title">T</span>, <span class="title">R</span>&gt; <span class="keyword">extends</span> <span class="title">Function</span> </span>&#123;</span><br><span class="line">    <span class="function">R <span class="title">call</span><span class="params">(T t)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>这段代码大家仔细一看就发现，其实<code>Operator</code>也是一个<code>Func1</code>嘛，只有一个<code>call</code>接口，用来把一个<code>Subscriber</code>转化成了另外一个<code>Subscriber</code>。</p>
<p>既然已经了解了这<code>Operator</code>，那么我们继续看<code>lift</code>到底做了什么。一步步看代码。</p>
<p>前面我介绍了一种方法去阅读源代码，就是我们已经知道了它的功能，就带着他这个功能去阅读源码，这里也是适用的。</p>
<p>以我开头的demo代码为例子，其实就是把<code>Subscriber&lt;List&lt;String&gt;&gt;</code>转化为了<code>Subscriber&lt;String&gt;</code></p>
<p>这里其实我自己理解起来都非常困难，为什么是把<code>Subscriber&lt;List&lt;String&gt;&gt;</code>转化为了<code>Subscriber&lt;String&gt;</code>而不是把<code>Subscriber&lt;String&gt;</code>转化为了<code>Subscriber&lt;List&lt;String&gt;&gt;</code></p>
<p>先不解释，先直接看<code>OperatorBufferWithSize</code>的代码</p>
<p>OperatorBufferWithSize<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line"><span class="keyword">public</span> Subscriber&lt;? <span class="keyword">super</span> T&gt; call(<span class="keyword">final</span> Subscriber&lt;? <span class="keyword">super</span> List&lt;T&gt;&gt; child) &#123;</span><br><span class="line">...</span><br><span class="line">&#125;</span><br><span class="line">...</span><br></pre></td></tr></table></figure></p>
<p>因为我们最习惯的思考逻辑就是从上到下，从左到右，因为一开始我们是<code>Test1</code>，<code>Test2</code>，<code>Test3</code>，然后呢组成，<code>[Test1，Test2]</code>，<code>[Test3]</code>这样子去处理，所以很正常的理解为把<code>Subscriber&lt;String&gt;</code>转化为了<code>Subscriber&lt;List&lt;String&gt;&gt;</code>。这也是为什么我觉得RxJava源码看起来比较累的原因。</p>
<p>在<a href="https://www.jianshu.com/p/03a72a5a2049" target="_blank" rel="noopener">前言</a>中介绍的那篇经典的文章中就有一幅图，我就不引用了，大家可以自己去看一下，RxJava的运行顺序是先下再上，再从上到下。我们直接继续从源码中找寻答案把。</p>
<p>Observable<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">final</span> &lt;R&gt; <span class="function">Observable&lt;R&gt; <span class="title">lift</span><span class="params">(<span class="keyword">final</span> Operator&lt;? extends R, ? <span class="keyword">super</span> T&gt; operator)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> unsafeCreate(<span class="keyword">new</span> OnSubscribeLift&lt;T, R&gt;(onSubscribe, operator));</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></p>
<p>OnSubscribeLift<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">(Subscriber&lt;? <span class="keyword">super</span> R&gt; o)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Subscriber&lt;? <span class="keyword">super</span> T&gt; st = RxJavaHooks.onObservableLift(operator).call(o);</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                st.onStart();</span><br><span class="line">                parent.call(st);</span><br><span class="line">            &#125;</span><br><span class="line">...</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></p>
<p><code>RxJavaHooks</code>我就不过多介绍了，在前几篇文章中已经有所解释，所以以上代码简化为<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">(Subscriber&lt;? <span class="keyword">super</span> R&gt; o)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Subscriber&lt;? <span class="keyword">super</span> T&gt; st = operator.call(o);</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                st.onStart();</span><br><span class="line">                parent.call(st);</span><br><span class="line">            &#125;</span><br><span class="line">...</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></p>
<p>当我们点击demo中的按钮，就是直接先调用上面的<code>call</code>方法。这里的<code>parent</code>其实是<code>Observable</code>中传入的<code>onSubscribe</code>，其实也就是前面所提到的<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Observable.OnSubscribe&lt;String&gt; &#123;</span><br><span class="line">            <span class="function">override fun <span class="title">call</span><span class="params">(t: Subscriber&lt;in String&gt;)</span> </span>&#123;</span><br><span class="line">                t.onNext(<span class="string">"test1"</span>)</span><br><span class="line">                t.onNext(<span class="string">"test2"</span>)</span><br><span class="line">                t.onNext(<span class="string">"test3"</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure></p>
<p>所以直接看到这里，其实大家应该可以看出来为什么是把<code>Subscriber&lt;List&lt;String&gt;&gt;</code>转化为<code>Subscriber&lt;String&gt;</code>了。大家再来重新看这段代码。</p>
<p>OnSubscribeLift<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">(Subscriber&lt;? <span class="keyword">super</span> R&gt; o)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Subscriber&lt;? <span class="keyword">super</span> T&gt; st = operator.call(o);</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                st.onStart();</span><br><span class="line">                parent.call(st);</span><br><span class="line">            &#125;</span><br><span class="line">...</span><br></pre></td></tr></table></figure></p>
<p>这里的参数<code>Subscriber&lt;? super R&gt; o</code>，就是我们在demo中<code>subscribe</code>订阅方法中传入的<code>Subscriber</code>。所以<code>R</code>其实就是<code>List&lt;String&gt;</code>，而在<code>call</code>方法中最终调用的是<code>parent.call</code>所传的参数是<code>Subscriber&lt;String&gt;</code>。调用的是下面这个方法<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Observable.OnSubscribe&lt;String&gt; &#123;</span><br><span class="line">            <span class="function">override fun <span class="title">call</span><span class="params">(t: Subscriber&lt;in String&gt;)</span> </span>&#123;</span><br><span class="line">                t.onNext(<span class="string">"test1"</span>)</span><br><span class="line">                t.onNext(<span class="string">"test2"</span>)</span><br><span class="line">                t.onNext(<span class="string">"test3"</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>所以我说，<code>RxJava</code>的执行方式呢，最后面的<code>OnSubscribe</code>的<code>call</code>先执行，然后再调用前面的<code>OnSubscribe</code>，最前面的<code>OnSubscribe</code>开始调用<code>onNext</code>方法，再一步一步往下执行。</p>
</blockquote>
<p>这里我们对整个方法的执行流程有了一个大致的了解，但是还没有开始去解析<code>buffer</code>操作符的真正实现的代码。</p>
<p>下面我们还是继续看</p>
<p>OperatorBufferWithSize<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line"> <span class="keyword">public</span> Subscriber&lt;? <span class="keyword">super</span> T&gt; call(<span class="keyword">final</span> Subscriber&lt;? <span class="keyword">super</span> List&lt;T&gt;&gt; child) &#123;</span><br><span class="line">        <span class="keyword">if</span> (skip == count) &#123;</span><br><span class="line">            BufferExact&lt;T&gt; parent = <span class="keyword">new</span> BufferExact&lt;T&gt;(child, count);</span><br><span class="line">            ...</span><br><span class="line">            <span class="keyword">return</span> parent;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (skip &gt; count) &#123;</span><br><span class="line">            BufferSkip&lt;T&gt; parent = <span class="keyword">new</span> BufferSkip&lt;T&gt;(child, count, skip);</span><br><span class="line">            ...</span><br><span class="line">            <span class="keyword">return</span> parent;</span><br><span class="line">        &#125;</span><br><span class="line">        BufferOverlap&lt;T&gt; parent = <span class="keyword">new</span> BufferOverlap&lt;T&gt;(child, count, skip);</span><br><span class="line">        ...</span><br><span class="line">        <span class="keyword">return</span> parent;</span><br><span class="line">    &#125;</span><br><span class="line">...</span><br></pre></td></tr></table></figure></p>
<p>这里非常明显的分为了3中情况，<code>skip</code>代表是忽略几个元素，<code>count</code>代表几个元素为1组。什么意思呢？<br>我们这里直接举个例子。<br>Observable.just(“Test1”,”Test2”,”Test3”,”Test4”,”Test5”)<br>元素多一点，大家好理解，那么我skip=2,count=2,那么就会被分为3组，”[Test1,Test2]”,”[Test3,Test4]”,”[Test5]”。如果skip=3,count=2,那么就会被分为2组，”[Test1,Test2]”，”[Test4,Test5]”。</p>
<p>大家自己尝试下，我就不举例了，我就想说明，这里分了3种情况做了不同处理，其实没什么关系，我们只需看明白一种就好了，其他2种也是大同小异的。我们的demo中<br><code>observable = Observable.just(&quot;test1&quot;, &quot;test2&quot;, &quot;test3&quot;).buffer(2)</code>只传了一个<code>2</code>，就是<code>skip==2&amp;&amp;count==2</code>。</p>
<p>所以我们直接看</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line">  <span class="keyword">if</span> (skip == count) &#123;</span><br><span class="line">            BufferExact&lt;T&gt; parent = <span class="keyword">new</span> BufferExact&lt;T&gt;(child, count);</span><br><span class="line">            ...</span><br><span class="line">            <span class="keyword">return</span> parent;</span><br><span class="line">        &#125;</span><br><span class="line">...</span><br><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">BufferExact</span>&lt;<span class="title">T</span>&gt; <span class="keyword">extends</span> <span class="title">Subscriber</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">        <span class="keyword">final</span> Subscriber&lt;? <span class="keyword">super</span> List&lt;T&gt;&gt; actual;</span><br><span class="line">        <span class="keyword">final</span> <span class="keyword">int</span> count;</span><br><span class="line"></span><br><span class="line">        List&lt;T&gt; buffer;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">BufferExact</span><span class="params">(Subscriber&lt;? <span class="keyword">super</span> List&lt;T&gt;&gt; actual, <span class="keyword">int</span> count)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">this</span>.actual = actual;</span><br><span class="line">            <span class="keyword">this</span>.count = count;</span><br><span class="line">            <span class="keyword">this</span>.request(<span class="number">0L</span>);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNext</span><span class="params">(T t)</span> </span>&#123;</span><br><span class="line">            List&lt;T&gt; b = buffer;</span><br><span class="line">            <span class="keyword">if</span> (b == <span class="keyword">null</span>) &#123;</span><br><span class="line">                b = <span class="keyword">new</span> ArrayList&lt;T&gt;(count);</span><br><span class="line">                buffer = b;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            b.add(t);</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (b.size() == count) &#123;</span><br><span class="line">                buffer = <span class="keyword">null</span>;</span><br><span class="line">                actual.onNext(b);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<p>前面介绍了<code>Operator</code>就是为了把 <code>Subscriber&lt;List&lt;String&gt;&gt;</code>转为 <code>Subscriber&lt;String&gt;</code>，而这里的<code>BufferExact</code>就是这个<code>Subscriber&lt;String&gt;</code>。<br>所以我们最主要的还是看<code>onNext</code>的实现。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNext</span><span class="params">(T t)</span> </span>&#123;</span><br><span class="line">            List&lt;T&gt; b = buffer;</span><br><span class="line">            <span class="keyword">if</span> (b == <span class="keyword">null</span>) &#123;</span><br><span class="line">                b = <span class="keyword">new</span> ArrayList&lt;T&gt;(count);</span><br><span class="line">                buffer = b;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            b.add(t);</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (b.size() == count) &#123;</span><br><span class="line">                buffer = <span class="keyword">null</span>;</span><br><span class="line">                actual.onNext(b);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
<p>实现非常的简单，先把onNext传递过来的数据先保存在 <code>List</code>中，等凑够了<code>count</code>数量后，在调用后面的<code>Subscriber&lt;List&lt;String&gt;&gt;</code>。</p>
<blockquote>
<p>所以总的来说<code>buffer</code>操作符的实现是简单的，但是由于涉及到了<code>lift</code>和<code>Operator</code>所以整体看起来还是比较复杂的。大家可以自己琢磨。</p>
</blockquote>
<h5 id="附加"><a href="#附加" class="headerlink" title="附加"></a>附加</h5><p>前面直接说<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"> Observable.create(object : Observable.OnSubscribe&lt;String&gt; &#123;</span><br><span class="line">            <span class="function">override fun <span class="title">call</span><span class="params">(t: Subscriber&lt;in String&gt;)</span> </span>&#123;</span><br><span class="line">                t.onNext(<span class="string">"test1"</span>)</span><br><span class="line">                t.onNext(<span class="string">"test2"</span>)</span><br><span class="line">                t.onNext(<span class="string">"test3"</span>)</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;)</span><br><span class="line">Observable.just(<span class="string">"test1"</span>, <span class="string">"test2"</span>, <span class="string">"test3"</span>)</span><br></pre></td></tr></table></figure></p>
<p>这2个是差不多的。其实我们可以再深入下。</p>
<p>Observable<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> &lt;T&gt; <span class="function">Observable&lt;T&gt; <span class="title">just</span><span class="params">(T t1, T t2, T t3)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> from((T[])<span class="keyword">new</span> Object[] &#123; t1, t2, t3 &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">...</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> &lt;T&gt; <span class="function">Observable&lt;T&gt; <span class="title">from</span><span class="params">(T[] array)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> n = array.length;</span><br><span class="line">        <span class="keyword">if</span> (n == <span class="number">0</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> empty();</span><br><span class="line">        &#125; <span class="keyword">else</span></span><br><span class="line">        <span class="keyword">if</span> (n == <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> just(array[<span class="number">0</span>]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> unsafeCreate(<span class="keyword">new</span> OnSubscribeFromArray&lt;T&gt;(array));</span><br><span class="line">    &#125;</span><br><span class="line">...</span><br></pre></td></tr></table></figure></p>
<p>其实就是创建了一个<code>OnSubscribeFromArray</code>，也就是一个<code>OnSubscribe</code>。继续深入</p>
<p>OnSubscribeFromArray<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">(Subscriber&lt;? <span class="keyword">super</span> T&gt; child)</span> </span>&#123;</span><br><span class="line">        child.setProducer(<span class="keyword">new</span> FromArrayProducer&lt;T&gt;(child, array));</span><br><span class="line">    &#125;</span><br><span class="line">..</span><br><span class="line"><span class="keyword">static</span> <span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">FromArrayProducer</span>&lt;<span class="title">T</span>&gt;</span></span><br><span class="line"><span class="class">    <span class="keyword">extends</span> <span class="title">AtomicLong</span></span></span><br><span class="line"><span class="class">    <span class="keyword">implements</span> <span class="title">Producer</span> </span>&#123;</span><br><span class="line">     ...</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">request</span><span class="params">(<span class="keyword">long</span> n)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">if</span> (n &lt; <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> IllegalArgumentException(<span class="string">"n &gt;= 0 required but it was "</span> + n);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (n == Long.MAX_VALUE) &#123;</span><br><span class="line">                <span class="keyword">if</span> (BackpressureUtils.getAndAddRequest(<span class="keyword">this</span>, n) == <span class="number">0</span>) &#123;</span><br><span class="line">                    fastPath();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; <span class="keyword">else</span></span><br><span class="line">            <span class="keyword">if</span> (n != <span class="number">0</span>) &#123;</span><br><span class="line">                <span class="keyword">if</span> (BackpressureUtils.getAndAddRequest(<span class="keyword">this</span>, n) == <span class="number">0</span>) &#123;</span><br><span class="line">                    slowPath(n);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">...</span><br></pre></td></tr></table></figure></p>
<p>如果是<code>OnSubscribe</code>其实我们主要就是看一个<code>call</code>方法，这里是直接<code>child.setProducer</code>，前面也介绍过，<code>setProducer</code>其实无法是直接调用<code>Producer</code>的<code>request</code>的方法。最后我们发现其实主要就看2个方法<code>fastPath</code>和<code>slowPath</code>。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">...</span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">fastPath</span><span class="params">()</span> </span>&#123;</span><br><span class="line">            <span class="keyword">final</span> Subscriber&lt;? <span class="keyword">super</span> T&gt; child = <span class="keyword">this</span>.child;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span> (T t : array) &#123;</span><br><span class="line">                <span class="keyword">if</span> (child.isUnsubscribed()) &#123;</span><br><span class="line">                    <span class="keyword">return</span>;</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                child.onNext(t);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> (child.isUnsubscribed()) &#123;</span><br><span class="line">                <span class="keyword">return</span>;</span><br><span class="line">            &#125;</span><br><span class="line">            child.onCompleted();</span><br><span class="line">        &#125;</span><br><span class="line">...</span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">slowPath</span><span class="params">(<span class="keyword">long</span> r)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">final</span> Subscriber&lt;? <span class="keyword">super</span> T&gt; child = <span class="keyword">this</span>.child;</span><br><span class="line">            <span class="keyword">final</span> T[] array = <span class="keyword">this</span>.array;</span><br><span class="line">            <span class="keyword">final</span> <span class="keyword">int</span> n = array.length;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">long</span> e = <span class="number">0L</span>;</span><br><span class="line">            <span class="keyword">int</span> i = index;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">for</span> (;;) &#123;</span><br><span class="line"></span><br><span class="line">                <span class="keyword">while</span> (r != <span class="number">0L</span> &amp;&amp; i != n) &#123;</span><br><span class="line">                    <span class="keyword">if</span> (child.isUnsubscribed()) &#123;</span><br><span class="line">                        <span class="keyword">return</span>;</span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                    child.onNext(array[i]);</span><br><span class="line"></span><br><span class="line">                    i++;</span><br><span class="line"></span><br><span class="line">                    <span class="keyword">if</span> (i == n) &#123;</span><br><span class="line">                        <span class="keyword">if</span> (!child.isUnsubscribed()) &#123;</span><br><span class="line">                            child.onCompleted();</span><br><span class="line">                        &#125;</span><br><span class="line">                        <span class="keyword">return</span>;</span><br><span class="line">                    &#125;</span><br><span class="line"></span><br><span class="line">                    r--;</span><br><span class="line">                    e--;</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                r = get() + e;</span><br><span class="line"></span><br><span class="line">                <span class="keyword">if</span> (r == <span class="number">0L</span>) &#123;</span><br><span class="line">                    index = i;</span><br><span class="line">                    r = addAndGet(e);</span><br><span class="line">                    <span class="keyword">if</span> (r == <span class="number">0L</span>) &#123;</span><br><span class="line">                        <span class="keyword">return</span>;</span><br><span class="line">                    &#125;</span><br><span class="line">                    e = <span class="number">0L</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">...</span><br></pre></td></tr></table></figure>
<p>其实直接看<code>fastPath</code>会更加清爽一点，就是遍历数组，然后挨个调用<code>onNext</code>,那么数组是什么呢，就是前面传入的<code>&quot;test1&quot;, &quot;test2&quot;, &quot;test3&quot;</code>。</p>
<p>至于<code>slowPath</code>呢其实关键代码也是<code>onNext</code>。可以先不深入去理解它具体的意思，其实看名字就可以看出来，一个是快速处理，一个是慢慢处理。</p>
<h5 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h5><p>整体下来我也已经尽量涉及到了每行代码，但是中间的跳转确实会让很多人发懵，如果觉得哪里讲的还不够清楚，可以指出，我可以修正。</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>Donate comment here</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/jexo/images/wechatpay.png" alt="Ling Hu 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/jexo/images/alipay.png" alt="Ling Hu 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/jexo/tags/Android/" rel="tag"># Android</a>
          
            <a href="/jexo/tags/RxJava/" rel="tag"># RxJava</a>
          
            <a href="/jexo/tags/源码解析/" rel="tag"># 源码解析</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/jexo/2018/04/18/看看谁最快的操作符-Amb/" rel="next" title="看看谁最快的操作符-----Amb">
                <i class="fa fa-chevron-left"></i> 看看谁最快的操作符-----Amb
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/jexo/2018/04/22/我是用来缓存的操作符-Cache/" rel="prev" title="我是用来缓存的操作符-----Cache">
                我是用来缓存的操作符-----Cache <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      
        <div id="gitment-container"></div>
      
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/jexo/images/avatar.jpg"
                alt="Ling Hu" />
            
              <p class="site-author-name" itemprop="name">Ling Hu</p>
              <p class="site-description motion-element" itemprop="description">Android,源码解析,每天进步一点点</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/jexo/archives/">
              
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/jexo/categories/index.html">
                  <span class="site-state-item-count">9</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/jexo/tags/index.html">
                  <span class="site-state-item-count">14</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/javalong" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-5"><a class="nav-link" href="#Buffer操作符功能介绍"><span class="nav-number">1.</span> <span class="nav-text">Buffer操作符功能介绍</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#用途"><span class="nav-number">2.</span> <span class="nav-text">用途</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#来一段代码"><span class="nav-number">3.</span> <span class="nav-text">来一段代码</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#先来学习下just是什么鬼"><span class="nav-number">4.</span> <span class="nav-text">先来学习下just是什么鬼</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#看看源代码"><span class="nav-number">5.</span> <span class="nav-text">看看源代码</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#附加"><span class="nav-number">6.</span> <span class="nav-text">附加</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#总结"><span class="nav-number">7.</span> <span class="nav-text">总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Ling Hu</span>

  
</div>









        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/jexo/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/jexo/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/jexo/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/jexo/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/jexo/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/jexo/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/jexo/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/jexo/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/jexo/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/jexo/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/jexo/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  







<!-- LOCAL: You can save these files to your site and update links -->
    
        
        <link rel="stylesheet" href="https://aimingoo.github.io/gitmint/style/default.css">
        <script src="https://aimingoo.github.io/gitmint/dist/gitmint.browser.js"></script>
    
<!-- END LOCAL -->

    
      <style>
        a.gitment-editor-footer-tip { display: none; }
        .gitment-container.gitment-footer-container { display: none; }
      </style>
    

    
      <script type="text/javascript">
      function renderGitment(){
        var gitment = new Gitmint({
            id: getId(),
            owner: 'javalong',
            repo: 'RxJava-Operator',
            
            lang: "zh-Hans" || navigator.language || navigator.systemLanguage || navigator.userLanguage,
            
            oauth: {
            
            
                client_secret: '06b042e57b8420f5002a6407348dde078bcde761',
            
                client_id: '1b5481d6512b2f28d7a2'
            }});
        gitment.render('gitment-container');
      }

      function getId(){
            if( window.location.pathname.length>=50){
                return window.location.pathname.substr(0,50)
            }else{
                return window.location.pathname
            }
      }

      
      renderGitment();
      
      </script>
    







  





  

  

  

  
  

  

  

  

</body>
</html>
